package com.guogee.pdb.dao;

import com.guogee.pdb.bean.Family;
import com.guogee.pdb.db.DBHelper.FamilyCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

public class FamilyDAO extends ADao<Family> {

	public FamilyDAO() {
		dbCollection = dbHelper.getIsmartDB().getCollection(FamilyCollection.COLLECTION_NAME);
	}

	@Override
	public boolean insertItem(Family obj) {
		if(insertItem1(obj)!=null){
			return true;
		}
		return false;
	}

	@Override
	public Family parstItemFromCursor(DBCursor cursor) {
		return parstDBObject(cursor.next());
	}
	
	public String insertItem1(Family obj) {
		DBObject dbObj = new BasicDBObject();
		dbObj.put(FamilyCollection.NAME, obj.getName());
		dbObj.put(FamilyCollection.CREATOR, obj.getCreator());
		dbObj.put(FamilyCollection.MOBLIE, obj.getMobile());
		dbObj.put(FamilyCollection.PASSWORD, obj.getPassword());
		dbObj.put(FamilyCollection.ADDR, obj.getAddr());
		dbObj.put(FamilyCollection.COMMUNITY_ID, obj.getCommunityId());
		dbObj.put(FamilyCollection.CREATE_TIME, obj.getCreateTime());
		dbObj.put(FamilyCollection.SERVICE_ID, obj.getServiceId());
		boolean ret = checkWritenResult(dbCollection.save(dbObj));
		if(ret){
			return dbObj.get(FamilyCollection.ID).toString();
		}else{
			return null;
		}
	}

	@Override
	public Family parstDBObject(DBObject object) {
		// TODO Auto-generated method stub
		Family obj = new Family();
		obj.setId(object.get(FamilyCollection.ID).toString());
		obj.setName(object.get(FamilyCollection.NAME).toString());
		obj.setCreator(object.get(FamilyCollection.CREATOR).toString());
		obj.setMobile(object.get(FamilyCollection.MOBLIE).toString());
		if(object.get(FamilyCollection.PASSWORD) != null){
			obj.setPassword(object.get(FamilyCollection.PASSWORD).toString());
		}
		if(object.get(FamilyCollection.ADDR) != null){
			obj.setAddr(object.get(FamilyCollection.ADDR).toString());
			obj.setCommunityId(object.get(FamilyCollection.COMMUNITY_ID).toString());
		}
		
		obj.setCreateTime(Long.valueOf(object.get(FamilyCollection.CREATE_TIME).toString()));
		if(object.get(FamilyCollection.SERVICE_ID) != null){
			obj.setServiceId(object.get(FamilyCollection.SERVICE_ID).toString());
		}
		
		return obj;
	}
}
